package sisbici.dao;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

public class SoporteDao {
	
	private EntityManagerFactory entityManagerFactory;
	private EntityManager entityManager;

	public SoporteDao() {
		super();
		this.entityManagerFactory = Persistence.createEntityManagerFactory("SisbiciUdistrital");
		this.entityManager = this.entityManagerFactory.createEntityManager();
	}


	public void setEntityManagerFactory(
			EntityManagerFactory entityManagerFactory) {

		if (entityManagerFactory != this.entityManagerFactory) {
			this.entityManagerFactory = entityManagerFactory;
		}

	}

	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	public final EntityManagerFactory getEntityManagerFactory() {
		return this.entityManagerFactory;
	}

	public final EntityManager getEntityManager() {
		return this.entityManager;
	}

	protected final void checkDaoConfig() {

		if (this.entityManagerFactory == null) {
			throw new IllegalArgumentException("entityManagerFactory is required");
		}
	}

	public List findByNamedQueryAndNamedParams(String query, Map<String, Object> params) {

		Query q = getEntityManager().createNamedQuery(query);

		Iterator iterator = params.keySet().iterator();
		while (iterator.hasNext()) {
			String key = (String) iterator.next();
			q.setParameter(key, params.get(key));
		}

		List<Object> listaResultado = null;
		try {
			listaResultado = (List<Object>) q.getResultList();
		} catch (Exception e) {
			System.out.println(e.toString());
		} catch (Throwable ex) {
			System.out.println(ex.toString());
		}

		return listaResultado;
	}
	/*public int DeleteByNamedQueryAndNamedParams(String query, Map<String, Object> params) {

		Query q = getEntityManager().createNamedQuery(query);
		try {
			int delete
		} catch (Exception e) {
			System.out.println(e.toString());
		} catch (Throwable ex) {
			System.out.println(ex.toString());
		}

		return listaResultado;
	}*/


	public List findByNamedQueryAndNamedParamsAndLimit(String query, Map<String, Object> params, Integer start, Integer finish) {

		Query q = getEntityManager().createNamedQuery(query).setFirstResult(start).setMaxResults(finish);

		Iterator iterator = params.keySet().iterator();
		while (iterator.hasNext()) {
			String key = (String) iterator.next();
			q.setParameter(key, params.get(key));
		}

		List<Object> listaResultado = null;
		try {
			listaResultado = (List<Object>) q.getResultList();
		} catch (Exception e) {
			System.out.println(e.toString());
		} catch (Throwable ex) {
			System.out.println(ex.toString());
		}

		return listaResultado;
	}

	public List findByNamedQuery(String query) {
		Query q = getEntityManager().createNamedQuery(query);
		return q.getResultList();
	}
	
	public boolean insertarEntidad(Object entidad) {
		try {
			getEntityManager().getTransaction().begin();
			getEntityManager().persist(entidad);
			getEntityManager().getTransaction().commit();
		} catch (Throwable t) {
			return false;
		}
		return true;
	}
	public boolean modificarEntidad(Object entidad) {
		try {
			getEntityManager().getTransaction().begin();
			getEntityManager().merge(entidad);
			getEntityManager().getTransaction().commit();
		} catch (Throwable t) {
			return false;
		}
		return true;
	}
	
	public Object encontrarEntidadPorId(Class<?> clase, String idEntidad) {
		return getEntityManager().find(clase, idEntidad);
	}

}
